package com.eachonline.smart.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eachonline.core.Pagination;
import com.eachonline.mapper.GenericMapper;
import com.eachonline.service.GenericServiceImpl;
import com.eachonline.smart.mapper.CertificatePictureMapper;
import com.eachonline.smart.mapper.SmartUserDetailMapper;
import com.eachonline.smart.mapper.SmartUserMapper;
import com.eachonline.smart.model.CertificatePicture;
import com.eachonline.smart.model.SmartCourse;
import com.eachonline.smart.model.SmartUser;
import com.eachonline.smart.model.SmartUserDetail;
import com.eachonline.smart.model.SmartUserExample;
import com.eachonline.smart.model.base.BaseSmartUserExample.Criteria;
import com.eachonline.smart.service.SmartUserService;
import com.eachonline.util.RedisHelper;
import com.eachonline.util.StringUtil;
@Service
public class SmartUserServiceImp extends GenericServiceImpl<SmartUser, SmartUserExample, String> implements SmartUserService {

	@Autowired
	private SmartUserMapper  smartUserMapper;
	
	@Autowired
	private SmartUserDetailMapper  userDetailMapper;
	
	@Autowired
	private CertificatePictureMapper  certPictureMapper;
	
	@Transactional
	@Override
	public void appleyUserCertificate(SmartUserDetail userDetail,
			List<CertificatePicture> certificatePicList) throws Exception {
		// TODO Auto-generated method stub
		userDetailMapper.insert(userDetail);
		certPictureMapper.insertBatchCertificatePic(certificatePicList);
	}
	
	
	
	public  boolean verificationCode(String telephone,String checkCode)
	{
		
		Object obj  = RedisHelper.get(telephone);
		boolean flag = false;
		if(obj != null)
		{
			String code = obj.toString();
			flag = code.equals(checkCode) ? true : false;
			
		}
		
		if(flag)
		{
			RedisHelper.del(telephone);
		}
		
		return flag;
	}



	@Override
	public Pagination<SmartUser> queryTeacherListBySite(String site, int start,
			int pageSize, String teacherName) {
		// TODO Auto-generated method stub
		SmartUserExample  userEx=new SmartUserExample();
		Criteria userCri=userEx.createCriteria();
		userCri.andUserRoleEqualTo("2").andCheckStatusEqualTo("2");
		if(StringUtil.isNotEmpty(site))
		{
			userCri.andUserSiteEqualTo(site);
		}
		if(StringUtil.isNotEmpty(teacherName))
		{
			userCri.andUserNameLike(teacherName);
		}	
		
		
		return queryByPage(userEx, start, pageSize, " create_time desc");
	}






	@Override
	protected GenericMapper<SmartUser, SmartUserExample, String> getGenericMapper() {
		// TODO Auto-generated method stub
		return this.smartUserMapper;
	}



	@Override
	public List<SmartCourse> queryMyCourses(String userId,int start,int pageSize) {
		// TODO Auto-generated method stub
		Pagination<SmartCourse >  page=new Pagination<SmartCourse>(start, pageSize);
		SmartUserExample example=new SmartUserExample();
		example.setUserId(userId);
		example.setPagination(page);
		
		return	smartUserMapper.queryMyStudyCourse(example);
	}



	@Override
	public List<SmartCourse> queryMyNoStudyCourse(String userId,int start,int pageSize,String courseType,String categroyCode) {
		// TODO Auto-generated method stub
		
		Pagination<SmartCourse >  page=new Pagination<SmartCourse>(start, pageSize);
		SmartUserExample example=new SmartUserExample();
		example.setUserId(userId);
		if(StringUtil.isNotEmpty(courseType))
		{
			example.setCourseType(courseType);
		}
		if(StringUtil.isNotEmpty(categroyCode))
		{
			example.setCategroyCode(categroyCode);
		}
		example.setPagination(page);
		return smartUserMapper.queryMyNoStudyCourse(example);
	}



	@Override
	public List<SmartCourse> queryMyDoneStudyCourse(String userId,int start,int pageSize,String courseType,String categroyCode) {
		// TODO Auto-generated method stub
		Pagination<SmartCourse >  page=new Pagination<SmartCourse>(start, pageSize);
		SmartUserExample example=new SmartUserExample();
		example.setUserId(userId);
		if(StringUtil.isNotEmpty(courseType))
		{
			example.setCourseType(courseType);
		}
		if(StringUtil.isNotEmpty(categroyCode))
		{
			example.setCategroyCode(categroyCode);
		}
		example.setPagination(page);
		return smartUserMapper.queryMyDoneStudyCourse(example);
	}



	@Override
	public List<SmartCourse> queryMyStudyingCourse(String userId,int start,int pageSize,String courseType,String categroyCode) {
		// TODO Auto-generated method stub
		Pagination<SmartCourse >  page=new Pagination<SmartCourse>(start, pageSize);
		SmartUserExample example=new SmartUserExample();
		example.setUserId(userId);
		if(StringUtil.isNotEmpty(courseType))
		{
			example.setCourseType(courseType);
		}
		
		if(StringUtil.isNotEmpty(categroyCode))
		{
			example.setCategroyCode(categroyCode);
		}
		
		
		example.setPagination(page);
		return smartUserMapper.queryMyStudyingCourse(example);
	}



	@Override
	public int queryMyNoStudyCourseCount(String userId,String courseType,String categroyCode) {
		// TODO Auto-generated method stub
		SmartUserExample example=new SmartUserExample();
		example.setUserId(userId);
		if(StringUtil.isNotEmpty(courseType))
		{
			example.setCourseType(courseType);
		}
		if(StringUtil.isNotEmpty(categroyCode))
		{
			example.setCategroyCode(categroyCode);
		}
		return smartUserMapper.queryMyNoStudyCourseCount(example);
	}


	@Override
	public int queryMyDoneStudyCourseCount(String userId,String courseType,String categroyCode) {
		// TODO Auto-generated method stub
		SmartUserExample example=new SmartUserExample();
		example.setUserId(userId);
		if(StringUtil.isNotEmpty(courseType))
		{
			example.setCourseType(courseType);
		}
		if(StringUtil.isNotEmpty(categroyCode))
		{
			example.setCategroyCode(categroyCode);
		}
		return smartUserMapper.queryMyDoneStudyCourseCount(example);
	}



	@Override
	public int queryMyStudyingCourseCount(String userId,String courseType,String categroyCode) {
		// TODO Auto-generated method stub
		SmartUserExample example=new SmartUserExample();
		example.setUserId(userId);
		if(StringUtil.isNotEmpty(courseType))
		{
			example.setCourseType(courseType);
		}
		if(StringUtil.isNotEmpty(categroyCode))
		{
			example.setCategroyCode(categroyCode);
		}
		
		return smartUserMapper.queryMyStudyingCourseCount(example);
	}
}